home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cctools / libstuff / bytesex.c < prev    next >
Text File  |  1994-12-13  |  53KB  |  1,700 lines

  1. /* byte_sex.c */
  2. #include <mach-o/fat.h>
  3. #include <mach-o/loader.h>
  4. #import <mach/m68k/thread_status.h>
  5. #import <mach/m98k/thread_status.h>
  6. #import <mach/m88k/thread_status.h>
  7. #import <mach/i860/thread_status.h>
  8. #import <mach/i386/thread_status.h>
  9. #import <mach/hppa/thread_status.h>
  10. #import <mach/sparc/thread_status.h>
  11. #include <mach-o/nlist.h>
  12. #include <mach-o/reloc.h>
  13. #include <bsd/ranlib.h>
  14. #include "stuff/bool.h"
  15. #include "stuff/bytesex.h"
  16.  
  17. double
  18. SWAP_DOUBLE(
  19. double d)
  20. {
  21.     union {
  22.         char c[8];
  23.         double d;
  24.     } in, out;
  25.     in.d = d;
  26.     out.c[0] = in.c[7];
  27.     out.c[1] = in.c[6];
  28.     out.c[2] = in.c[5];
  29.     out.c[3] = in.c[4];
  30.     out.c[4] = in.c[3];
  31.     out.c[5] = in.c[2];
  32.     out.c[6] = in.c[1];
  33.     out.c[7] = in.c[0];
  34.     return(out.d);
  35. }
  36.  
  37. float
  38. SWAP_FLOAT(
  39. float f)
  40. {
  41.     union {
  42.         char c[7];
  43.         float f;
  44.     } in, out;
  45.     in.f = f;
  46.     out.c[0] = in.c[3];
  47.     out.c[1] = in.c[2];
  48.     out.c[2] = in.c[1];
  49.     out.c[3] = in.c[0];
  50.     return(out.f);
  51. }
  52.  
  53. /*
  54.  * get_host_byte_sex() returns the enum constant for the byte sex of the host
  55.  * it is running on.
  56.  */
  57. enum byte_sex
  58. get_host_byte_sex(
  59. void)
  60. {
  61.     unsigned long s;
  62.  
  63.     s = (BIG_ENDIAN_BYTE_SEX << 24) | LITTLE_ENDIAN_BYTE_SEX;
  64.     return((enum byte_sex)*((char *)&s));
  65. }
  66.  
  67. void
  68. swap_fat_header(
  69. struct fat_header *fat_header,
  70. enum byte_sex target_byte_sex)
  71. {
  72.     fat_header->magic     = SWAP_LONG(fat_header->magic);
  73.     fat_header->nfat_arch = SWAP_LONG(fat_header->nfat_arch);
  74. }
  75.  
  76. void
  77. swap_fat_arch(
  78. struct fat_arch *fat_archs,
  79. unsigned long nfat_arch,
  80. enum byte_sex target_byte_sex)
  81. {
  82.     unsigned long i;
  83.  
  84.     for(i = 0; i < nfat_arch; i++){
  85.         fat_archs[i].cputype    = SWAP_LONG(fat_archs[i].cputype);
  86.         fat_archs[i].cpusubtype = SWAP_LONG(fat_archs[i].cpusubtype);
  87.         fat_archs[i].offset     = SWAP_LONG(fat_archs[i].offset);
  88.         fat_archs[i].size       = SWAP_LONG(fat_archs[i].size);
  89.         fat_archs[i].align      = SWAP_LONG(fat_archs[i].align);
  90.     }
  91. }
  92.  
  93. void
  94. swap_mach_header(
  95. struct mach_header *mh,
  96. enum byte_sex target_byte_sex)
  97. {
  98.     mh->magic = SWAP_LONG(mh->magic);
  99.     mh->cputype = SWAP_LONG(mh->cputype);
  100.     mh->cpusubtype = SWAP_LONG(mh->cpusubtype);
  101.     mh->filetype = SWAP_LONG(mh->filetype);
  102.     mh->ncmds = SWAP_LONG(mh->ncmds);
  103.     mh->sizeofcmds = SWAP_LONG(mh->sizeofcmds);
  104.     mh->flags = SWAP_LONG(mh->flags);
  105. }
  106.  
  107. void
  108. swap_load_command(
  109. struct load_command *lc,
  110. enum byte_sex target_byte_sex)
  111. {
  112.     lc->cmd = SWAP_LONG(lc->cmd);
  113.     lc->cmdsize = SWAP_LONG(lc->cmdsize);
  114. }
  115.  
  116. void
  117. swap_segment_command(
  118. struct segment_command *sg,
  119. enum byte_sex target_byte_sex)
  120. {
  121.     /* segname[16] */
  122.     sg->cmd = SWAP_LONG(sg->cmd);
  123.     sg->cmdsize = SWAP_LONG(sg->cmdsize);
  124.     sg->vmaddr = SWAP_LONG(sg->vmaddr);
  125.     sg->vmsize = SWAP_LONG(sg->vmsize);
  126.     sg->fileoff = SWAP_LONG(sg->fileoff);
  127.     sg->filesize = SWAP_LONG(sg->filesize);
  128.     sg->maxprot = SWAP_LONG(sg->maxprot);
  129.     sg->initprot = SWAP_LONG(sg->initprot);
  130.     sg->nsects = SWAP_LONG(sg->nsects);
  131.     sg->flags = SWAP_LONG(sg->flags);
  132. }
  133.  
  134. void
  135. swap_section(
  136. struct section *s,
  137. unsigned long nsects,
  138. enum byte_sex target_byte_sex)
  139. {
  140.     unsigned long i;
  141.  
  142.     for(i = 0; i < nsects; i++){
  143.         /* sectname[16] */
  144.         /* segname[16] */
  145.         s[i].addr = SWAP_LONG(s[i].addr);
  146.         s[i].size = SWAP_LONG(s[i].size);
  147.         s[i].offset = SWAP_LONG(s[i].offset);
  148.         s[i].align = SWAP_LONG(s[i].align);
  149.         s[i].reloff = SWAP_LONG(s[i].reloff);
  150.         s[i].nreloc = SWAP_LONG(s[i].nreloc);
  151.         s[i].flags = SWAP_LONG(s[i].flags);
  152.         s[i].reserved1 = SWAP_LONG(s[i].reserved1);
  153.         s[i].reserved2 = SWAP_LONG(s[i].reserved2);
  154.     }
  155. }
  156.  
  157. void
  158. swap_symtab_command(
  159. struct symtab_command *st,
  160. enum byte_sex target_byte_sex)
  161. {
  162.     st->cmd = SWAP_LONG(st->cmd);
  163.     st->cmdsize = SWAP_LONG(st->cmdsize);
  164.     st->symoff = SWAP_LONG(st->symoff);
  165.     st->nsyms = SWAP_LONG(st->nsyms);
  166.     st->stroff = SWAP_LONG(st->stroff);
  167.     st->strsize = SWAP_LONG(st->strsize);
  168. }
  169.  
  170. void
  171. swap_dysymtab_command(
  172. struct dysymtab_command *dyst,
  173. enum byte_sex target_byte_sex)
  174. {
  175.     dyst->cmd = SWAP_LONG(dyst->cmd);
  176.     dyst->cmdsize = SWAP_LONG(dyst->cmdsize);
  177.     dyst->ilocalsym = SWAP_LONG(dyst->ilocalsym);
  178.     dyst->nlocalsym = SWAP_LONG(dyst->nlocalsym);
  179.     dyst->iextdefsym = SWAP_LONG(dyst->iextdefsym);
  180.     dyst->nextdefsym = SWAP_LONG(dyst->nextdefsym);
  181.     dyst->iundefsym = SWAP_LONG(dyst->iundefsym);
  182.     dyst->nundefsym = SWAP_LONG(dyst->nundefsym);
  183.     dyst->tocoff = SWAP_LONG(dyst->tocoff);
  184.     dyst->ntoc = SWAP_LONG(dyst->ntoc);
  185.     dyst->modtaboff = SWAP_LONG(dyst->modtaboff);
  186.     dyst->nmodtab = SWAP_LONG(dyst->nmodtab);
  187.     dyst->extrefsymoff = SWAP_LONG(dyst->extrefsymoff);
  188.     dyst->nextrefsyms = SWAP_LONG(dyst->nextrefsyms);
  189.     dyst->indirectsymoff = SWAP_LONG(dyst->indirectsymoff);
  190.     dyst->nindirectsyms = SWAP_LONG(dyst->nindirectsyms);
  191.     dyst->extreloff = SWAP_LONG(dyst->extreloff);
  192.     dyst->nextrel = SWAP_LONG(dyst->nextrel);
  193.     dyst->locreloff = SWAP_LONG(dyst->locreloff);
  194.     dyst->nlocrel = SWAP_LONG(dyst->nlocrel);
  195. }
  196.  
  197. void
  198. swap_symseg_command(
  199. struct symseg_command *ss,
  200. enum byte_sex target_byte_sex)
  201. {
  202.     ss->cmd = SWAP_LONG(ss->cmd);
  203.     ss->cmdsize = SWAP_LONG(ss->cmdsize);
  204.     ss->offset = SWAP_LONG(ss->offset);
  205.     ss->size = SWAP_LONG(ss->size);
  206. }
  207.  
  208. void
  209. swap_fvmlib_command(
  210. struct fvmlib_command *fl,
  211. enum byte_sex target_byte_sex)
  212. {
  213.     fl->cmd = SWAP_LONG(fl->cmd);
  214.     fl->cmdsize = SWAP_LONG(fl->cmdsize);
  215.     fl->fvmlib.name.offset = SWAP_LONG(fl->fvmlib.name.offset);
  216.     fl->fvmlib.minor_version = SWAP_LONG(fl->fvmlib.minor_version);
  217.     fl->fvmlib.header_addr = SWAP_LONG(fl->fvmlib.header_addr);
  218. }
  219.  
  220. void
  221. swap_dylib_command(
  222. struct dylib_command *dl,
  223. enum byte_sex target_byte_sex)
  224. {
  225.     dl->cmd = SWAP_LONG(dl->cmd);
  226.     dl->cmdsize = SWAP_LONG(dl->cmdsize);
  227.     dl->dylib.name.offset = SWAP_LONG(dl->dylib.name.offset);
  228.     dl->dylib.timestamp = SWAP_LONG(dl->dylib.timestamp);
  229.     dl->dylib.current_version = SWAP_LONG(dl->dylib.current_version);
  230.     dl->dylib.compatibility_version =
  231.                 SWAP_LONG(dl->dylib.compatibility_version);
  232. }
  233.  
  234. void
  235. swap_dylinker_command(
  236. struct dylinker_command *dyld,
  237. enum byte_sex target_byte_sex)
  238. {
  239.     dyld->cmd = SWAP_LONG(dyld->cmd);
  240.     dyld->cmdsize = SWAP_LONG(dyld->cmdsize);
  241.     dyld->name.offset = SWAP_LONG(dyld->name.offset);
  242. }
  243.  
  244. void
  245. swap_fvmfile_command(
  246. struct fvmfile_command *ff,
  247. enum byte_sex target_byte_sex)
  248. {
  249.     ff->cmd = SWAP_LONG(ff->cmd);
  250.     ff->cmdsize = SWAP_LONG(ff->cmdsize);
  251.     ff->name.offset = SWAP_LONG(ff->name.offset);
  252.     ff->header_addr = SWAP_LONG(ff->header_addr);
  253. }
  254.  
  255.  
  256. void
  257. swap_thread_command(
  258. struct thread_command *ut,
  259. enum byte_sex target_byte_sex)
  260. {
  261.     ut->cmd = SWAP_LONG(ut->cmd);
  262.     ut->cmdsize = SWAP_LONG(ut->cmdsize);
  263. }
  264.  
  265. void
  266. swap_m68k_thread_state_regs(
  267. struct m68k_thread_state_regs *cpu,
  268. enum byte_sex target_byte_sex)
  269. {
  270.     unsigned long i;
  271.  
  272.     for(i = 0; i < 8; i++)
  273.         cpu->dreg[i] = SWAP_LONG(cpu->dreg[i]);
  274.     for(i = 0; i < 8; i++)
  275.         cpu->areg[i] = SWAP_LONG(cpu->areg[i]);
  276.     cpu->pad0 = SWAP_SHORT(cpu->pad0);
  277.     cpu->sr = SWAP_SHORT(cpu->sr);
  278.     cpu->pc = SWAP_LONG(cpu->pc);
  279. }
  280.  
  281. void
  282. swap_m68k_thread_state_68882(
  283. struct m68k_thread_state_68882 *fpu,
  284. enum byte_sex target_byte_sex)
  285. {
  286.     unsigned long i, tmp;
  287.  
  288.     for(i = 0; i < 8; i++){
  289.                        tmp = SWAP_LONG(fpu->regs[i].fp[0]);
  290.         fpu->regs[i].fp[1] = SWAP_LONG(fpu->regs[i].fp[1]);
  291.         fpu->regs[i].fp[0] = SWAP_LONG(fpu->regs[i].fp[2]);
  292.         fpu->regs[i].fp[2] = tmp;
  293.     }
  294.     fpu->cr = SWAP_LONG(fpu->cr);
  295.     fpu->sr = SWAP_LONG(fpu->sr);
  296.     fpu->iar = SWAP_LONG(fpu->iar);
  297.     fpu->state = SWAP_LONG(fpu->state);
  298. }
  299.  
  300. void
  301. swap_m68k_thread_state_user_reg(
  302. struct m68k_thread_state_user_reg *user_reg,
  303. enum byte_sex target_byte_sex)
  304. {
  305.     user_reg->user_reg = SWAP_LONG(user_reg->user_reg);
  306. }
  307.  
  308. void
  309. swap_m98k_thread_state_grf_t(
  310. m98k_thread_state_grf_t *cpu,
  311. enum byte_sex target_byte_sex)
  312. {
  313.     enum byte_sex host_byte_sex;
  314.  
  315.     struct swapped_m98k_cr {
  316.     union {
  317.         struct {
  318.         unsigned rsvd:BITS_WIDTH(23,0);
  319.         unsigned ox:BIT_WIDTH(24);
  320.         unsigned vx:BIT_WIDTH(25);
  321.         unsigned fex:BIT_WIDTH(26);
  322.         unsigned fx:BIT_WIDTH(27);
  323.         unsigned so:BIT_WIDTH(28);
  324.         unsigned eq:BIT_WIDTH(29);
  325.         unsigned gt:BIT_WIDTH(30);
  326.         unsigned lt:BIT_WIDTH(31);
  327.         } fields;
  328.         unsigned long word;
  329.     } u;
  330.     } scr;
  331.     struct swapped_m98k_xer {
  332.     union {
  333.         struct {
  334.         unsigned byte_count:BITS_WIDTH(6,0);    
  335.         unsigned rsvd2:BIT_WIDTH(7);
  336.         unsigned byte:BITS_WIDTH(15,8);
  337.         unsigned rsvd1:BITS_WIDTH(28,16);
  338.         unsigned ca:BIT_WIDTH(29);
  339.         unsigned ov:BIT_WIDTH(30);
  340.         unsigned so:BIT_WIDTH(31);
  341.         } fields;
  342.         unsigned long word;
  343.     } u;
  344.     } sxer;
  345.     struct swapped_m98k_msr {
  346.     union {
  347.         struct {
  348.         unsigned psfr:BIT_WIDTH(0);
  349.         unsigned rsvd2:BITS_WIDTH(3,1);
  350.         unsigned dr:BIT_WIDTH(4);
  351.         unsigned ir:BIT_WIDTH(5);
  352.         unsigned ip:BIT_WIDTH(6);
  353.         unsigned rsvd1:BIT_WIDTH(7);
  354.         unsigned fe1:BIT_WIDTH(8);
  355.         unsigned be:BIT_WIDTH(9);
  356.         unsigned se:BIT_WIDTH(10);
  357.         unsigned fe0:BIT_WIDTH(11);
  358.         unsigned me:BIT_WIDTH(12);
  359.         unsigned fp:BIT_WIDTH(13);
  360.         unsigned pr:BIT_WIDTH(14);
  361.         unsigned ee:BIT_WIDTH(15);
  362.         unsigned rsvd3:BITS_WIDTH(31,16);
  363.         } fields;
  364.         unsigned long word;
  365.     } u;
  366.     } smsr;
  367.  
  368.     host_byte_sex = get_host_byte_sex();
  369.  
  370.     cpu->r0 = SWAP_LONG(cpu->r0);
  371.     cpu->r1 = SWAP_LONG(cpu->r1);
  372.     cpu->r2 = SWAP_LONG(cpu->r2);
  373.     cpu->r3 = SWAP_LONG(cpu->r3);
  374.     cpu->r4 = SWAP_LONG(cpu->r4);
  375.     cpu->r5 = SWAP_LONG(cpu->r5);
  376.     cpu->r6 = SWAP_LONG(cpu->r6);
  377.     cpu->r7 = SWAP_LONG(cpu->r7);
  378.     cpu->r8 = SWAP_LONG(cpu->r8);
  379.     cpu->r9 = SWAP_LONG(cpu->r9);
  380.     cpu->r10 = SWAP_LONG(cpu->r10);
  381.     cpu->r11 = SWAP_LONG(cpu->r11);
  382.     cpu->r12 = SWAP_LONG(cpu->r12);
  383.     cpu->r13 = SWAP_LONG(cpu->r13);
  384.     cpu->r14 = SWAP_LONG(cpu->r14);
  385.     cpu->r15 = SWAP_LONG(cpu->r15);
  386.     cpu->r16 = SWAP_LONG(cpu->r16);
  387.     cpu->r17 = SWAP_LONG(cpu->r17);
  388.     cpu->r18 = SWAP_LONG(cpu->r18);
  389.     cpu->r19 = SWAP_LONG(cpu->r19);
  390.     cpu->r20 = SWAP_LONG(cpu->r20);
  391.     cpu->r21 = SWAP_LONG(cpu->r21);
  392.     cpu->r22 = SWAP_LONG(cpu->r22);
  393.     cpu->r23 = SWAP_LONG(cpu->r23);
  394.     cpu->r24 = SWAP_LONG(cpu->r24);
  395.     cpu->r25 = SWAP_LONG(cpu->r25);
  396.     cpu->r26 = SWAP_LONG(cpu->r26);
  397.     cpu->r27 = SWAP_LONG(cpu->r27);
  398.     cpu->r28 = SWAP_LONG(cpu->r28);
  399.     cpu->r29 = SWAP_LONG(cpu->r29);
  400.     cpu->r30 = SWAP_LONG(cpu->r30);
  401.     cpu->r31 = SWAP_LONG(cpu->r31);
  402.     cpu->lr  = SWAP_LONG(cpu->lr);
  403.     cpu->ctr = SWAP_LONG(cpu->ctr);
  404.     cpu->cia = SWAP_LONG(cpu->cia);
  405.  
  406.     if(target_byte_sex == host_byte_sex){
  407.         memcpy(&scr, &(cpu->cr), sizeof(struct swapped_m98k_cr));
  408.         scr.u.word = SWAP_LONG(scr.u.word);
  409.         cpu->cr.lt = scr.u.fields.lt;
  410.         cpu->cr.gt = scr.u.fields.gt;
  411.         cpu->cr.eq = scr.u.fields.eq;
  412.         cpu->cr.so = scr.u.fields.so;
  413.         cpu->cr.fx = scr.u.fields.fx;
  414.         cpu->cr.fex = scr.u.fields.fex;
  415.         cpu->cr.vx = scr.u.fields.vx;
  416.         cpu->cr.ox = scr.u.fields.ox;
  417.         cpu->cr.rsvd = scr.u.fields.rsvd;
  418.  
  419.         memcpy(&sxer, &(cpu->xer), sizeof(struct swapped_m98k_xer));
  420.         sxer.u.word = SWAP_LONG(sxer.u.word);
  421.         cpu->xer.byte_count = sxer.u.fields.byte_count;
  422.         cpu->xer.rsvd2 = sxer.u.fields.rsvd2;
  423.         cpu->xer.byte = sxer.u.fields.byte;
  424.         cpu->xer.rsvd1 = sxer.u.fields.rsvd1;
  425.         cpu->xer.ca = sxer.u.fields.ca;
  426.         cpu->xer.ov = sxer.u.fields.ov;
  427.         cpu->xer.so = sxer.u.fields.so;
  428.  
  429.         memcpy(&smsr, &(cpu->msr), sizeof(struct swapped_m98k_msr));
  430.         smsr.u.word = SWAP_LONG(smsr.u.word);
  431.         cpu->msr.psfr = smsr.u.fields.psfr;
  432.         cpu->msr.rsvd2 = smsr.u.fields.rsvd2;
  433.         cpu->msr.dr = smsr.u.fields.dr;
  434.         cpu->msr.ir = smsr.u.fields.ir;
  435.         cpu->msr.ip = smsr.u.fields.ip;
  436.         cpu->msr.rsvd1 = smsr.u.fields.rsvd1;
  437.         cpu->msr.fe1 = smsr.u.fields.fe1;
  438.         cpu->msr.be = smsr.u.fields.be;
  439.         cpu->msr.se = smsr.u.fields.se;
  440.         cpu->msr.fe0 = smsr.u.fields.fe0;
  441.         cpu->msr.me = smsr.u.fields.me;
  442.         cpu->msr.fp = smsr.u.fields.fp;
  443.         cpu->msr.pr = smsr.u.fields.pr;
  444.         cpu->msr.ee = smsr.u.fields.ee;
  445.         cpu->msr.rsvd3 = smsr.u.fields.rsvd3;
  446.     }
  447.     else{
  448.         scr.u.fields.lt = cpu->cr.lt;
  449.         scr.u.fields.gt = cpu->cr.gt;
  450.         scr.u.fields.eq = cpu->cr.eq;
  451.         scr.u.fields.so = cpu->cr.so;
  452.         scr.u.fields.fx = cpu->cr.fx;
  453.         scr.u.fields.fex = cpu->cr.fex;
  454.         scr.u.fields.vx = cpu->cr.vx;
  455.         scr.u.fields.ox = cpu->cr.ox;
  456.         scr.u.fields.rsvd = cpu->cr.rsvd;
  457.         scr.u.word = SWAP_LONG(scr.u.word);
  458.         memcpy(&(cpu->cr), &scr, sizeof(struct swapped_m98k_cr));
  459.  
  460.         sxer.u.fields.byte_count = cpu->xer.byte_count;
  461.         sxer.u.fields.rsvd2 = cpu->xer.rsvd2;
  462.         sxer.u.fields.byte = cpu->xer.byte;
  463.         sxer.u.fields.rsvd1 = cpu->xer.rsvd1;
  464.         sxer.u.fields.ca = cpu->xer.ca;
  465.         sxer.u.fields.ov = cpu->xer.ov;
  466.         sxer.u.fields.so = cpu->xer.so;
  467.         sxer.u.word = SWAP_LONG(sxer.u.word);
  468.         memcpy(&(cpu->xer), &sxer, sizeof(struct swapped_m98k_xer));
  469.  
  470.         smsr.u.fields.psfr = cpu->msr.psfr;
  471.         smsr.u.fields.rsvd2 = cpu->msr.rsvd2;
  472.         smsr.u.fields.dr = cpu->msr.dr;
  473.         smsr.u.fields.ir = cpu->msr.ir;
  474.         smsr.u.fields.ip = cpu->msr.ip;
  475.         smsr.u.fields.rsvd1 = cpu->msr.rsvd1;
  476.         smsr.u.fields.fe1 = cpu->msr.fe1;
  477.         smsr.u.fields.be = cpu->msr.be;
  478.         smsr.u.fields.se = cpu->msr.se;
  479.         smsr.u.fields.fe0 = cpu->msr.fe0;
  480.         smsr.u.fields.me = cpu->msr.me;
  481.         smsr.u.fields.fp = cpu->msr.fp;
  482.         smsr.u.fields.pr = cpu->msr.pr;
  483.         smsr.u.fields.ee = cpu->msr.ee;
  484.         smsr.u.fields.rsvd3 = cpu->msr.rsvd3;
  485.         smsr.u.word = SWAP_LONG(smsr.u.word);
  486.         memcpy(&(cpu->msr), &smsr, sizeof(struct swapped_m98k_msr));
  487.     }
  488. }
  489.  
  490. void
  491. swap_m88k_thread_state_grf_t(
  492. m88k_thread_state_grf_t *cpu,
  493. enum byte_sex target_byte_sex)
  494. {
  495.     cpu->r1 = SWAP_LONG(cpu->r1);
  496.     cpu->r2 = SWAP_LONG(cpu->r2);
  497.     cpu->r3 = SWAP_LONG(cpu->r3);
  498.     cpu->r4 = SWAP_LONG(cpu->r4);
  499.     cpu->r5 = SWAP_LONG(cpu->r5);
  500.     cpu->r6 = SWAP_LONG(cpu->r6);
  501.     cpu->r7 = SWAP_LONG(cpu->r7);
  502.     cpu->r8 = SWAP_LONG(cpu->r8);
  503.     cpu->r9 = SWAP_LONG(cpu->r9);
  504.     cpu->r10 = SWAP_LONG(cpu->r10);
  505.     cpu->r11 = SWAP_LONG(cpu->r11);
  506.     cpu->r12 = SWAP_LONG(cpu->r12);
  507.     cpu->r13 = SWAP_LONG(cpu->r13);
  508.     cpu->r14 = SWAP_LONG(cpu->r14);
  509.     cpu->r15 = SWAP_LONG(cpu->r15);
  510.     cpu->r16 = SWAP_LONG(cpu->r16);
  511.     cpu->r17 = SWAP_LONG(cpu->r17);
  512.     cpu->r18 = SWAP_LONG(cpu->r18);
  513.     cpu->r19 = SWAP_LONG(cpu->r19);
  514.     cpu->r20 = SWAP_LONG(cpu->r20);
  515.     cpu->r21 = SWAP_LONG(cpu->r21);
  516.     cpu->r22 = SWAP_LONG(cpu->r22);
  517.     cpu->r23 = SWAP_LONG(cpu->r23);
  518.     cpu->r24 = SWAP_LONG(cpu->r24);
  519.     cpu->r25 = SWAP_LONG(cpu->r25);
  520.     cpu->r26 = SWAP_LONG(cpu->r26);
  521.     cpu->r27 = SWAP_LONG(cpu->r27);
  522.     cpu->r28 = SWAP_LONG(cpu->r28);
  523.     cpu->r29 = SWAP_LONG(cpu->r29);
  524.     cpu->r30 = SWAP_LONG(cpu->r30);
  525.     cpu->r31 = SWAP_LONG(cpu->r31);
  526.     cpu->xip = SWAP_LONG(cpu->xip);
  527.     cpu->xip_in_bd = SWAP_LONG(cpu->xip_in_bd);
  528.     cpu->nip = SWAP_LONG(cpu->nip);
  529. }
  530.  
  531. void
  532. swap_m88k_thread_state_xrf_t(
  533. m88k_thread_state_xrf_t *fpu,
  534. enum byte_sex target_byte_sex)
  535. {
  536.     enum byte_sex host_byte_sex;
  537.  
  538.     struct swapped_m88k_fpsr {
  539.     union {
  540.         struct {
  541.         unsigned    afinx:BIT_WIDTH(0);
  542.         unsigned    afovf:BIT_WIDTH(1);
  543.         unsigned    afunf:BIT_WIDTH(2);
  544.         unsigned    afdvz:BIT_WIDTH(3);
  545.         unsigned    afinv:BIT_WIDTH(4);
  546.         unsigned    :BITS_WIDTH(15,5);
  547.         unsigned    xmod:BIT_WIDTH(16);
  548.         unsigned    :BITS_WIDTH(31,17);
  549.         } fields;
  550.         unsigned long word;
  551.     } u;
  552.     } ssr;
  553.     struct swapped_m88k_fpcr {
  554.     union {
  555.         struct {
  556.         unsigned    efinx:BIT_WIDTH(0);
  557.         unsigned    efovf:BIT_WIDTH(1);
  558.         unsigned    efunf:BIT_WIDTH(2);
  559.         unsigned    efdvz:BIT_WIDTH(3);
  560.         unsigned    efinv:BIT_WIDTH(4);
  561.         unsigned    :BITS_WIDTH(13,5);
  562.         m88k_fpcr_rm_t    rm:BITS_WIDTH(15,14);
  563.         unsigned    :BITS_WIDTH(31,16);
  564.         } fields;
  565.         unsigned long word;
  566.     } u;
  567.     } scr;
  568.  
  569.     host_byte_sex = get_host_byte_sex();
  570.  
  571.     fpu->x1.x[0] = SWAP_LONG(fpu->x1.x[0]);
  572.     fpu->x1.x[1] = SWAP_LONG(fpu->x1.x[1]);
  573.     fpu->x1.x[2] = SWAP_LONG(fpu->x1.x[2]);
  574.     fpu->x1.x[3] = SWAP_LONG(fpu->x1.x[3]);
  575.     fpu->x2.x[0] = SWAP_LONG(fpu->x2.x[0]);
  576.     fpu->x2.x[1] = SWAP_LONG(fpu->x2.x[1]);
  577.     fpu->x2.x[2] = SWAP_LONG(fpu->x2.x[2]);
  578.     fpu->x2.x[3] = SWAP_LONG(fpu->x2.x[3]);
  579.     fpu->x3.x[0] = SWAP_LONG(fpu->x3.x[0]);
  580.     fpu->x3.x[1] = SWAP_LONG(fpu->x3.x[1]);
  581.     fpu->x3.x[2] = SWAP_LONG(fpu->x3.x[2]);
  582.     fpu->x3.x[3] = SWAP_LONG(fpu->x3.x[3]);
  583.     fpu->x4.x[0] = SWAP_LONG(fpu->x4.x[0]);
  584.     fpu->x4.x[1] = SWAP_LONG(fpu->x4.x[1]);
  585.     fpu->x4.x[2] = SWAP_LONG(fpu->x4.x[2]);
  586.     fpu->x4.x[3] = SWAP_LONG(fpu->x4.x[3]);
  587.     fpu->x5.x[0] = SWAP_LONG(fpu->x5.x[0]);
  588.     fpu->x5.x[1] = SWAP_LONG(fpu->x5.x[1]);
  589.     fpu->x5.x[2] = SWAP_LONG(fpu->x5.x[2]);
  590.     fpu->x5.x[3] = SWAP_LONG(fpu->x5.x[3]);
  591.     fpu->x6.x[0] = SWAP_LONG(fpu->x6.x[0]);
  592.     fpu->x6.x[1] = SWAP_LONG(fpu->x6.x[1]);
  593.     fpu->x6.x[2] = SWAP_LONG(fpu->x6.x[2]);
  594.     fpu->x6.x[3] = SWAP_LONG(fpu->x6.x[3]);
  595.     fpu->x7.x[0] = SWAP_LONG(fpu->x7.x[0]);
  596.     fpu->x7.x[1] = SWAP_LONG(fpu->x7.x[1]);
  597.     fpu->x7.x[2] = SWAP_LONG(fpu->x7.x[2]);
  598.     fpu->x7.x[3] = SWAP_LONG(fpu->x7.x[3]);
  599.     fpu->x8.x[0] = SWAP_LONG(fpu->x8.x[0]);
  600.     fpu->x8.x[1] = SWAP_LONG(fpu->x8.x[1]);
  601.     fpu->x8.x[2] = SWAP_LONG(fpu->x8.x[2]);
  602.     fpu->x8.x[3] = SWAP_LONG(fpu->x8.x[3]);
  603.     fpu->x9.x[0] = SWAP_LONG(fpu->x9.x[0]);
  604.     fpu->x9.x[1] = SWAP_LONG(fpu->x9.x[1]);
  605.     fpu->x9.x[2] = SWAP_LONG(fpu->x9.x[2]);
  606.     fpu->x9.x[3] = SWAP_LONG(fpu->x9.x[3]);
  607.     fpu->x10.x[0] = SWAP_LONG(fpu->x10.x[0]);
  608.     fpu->x10.x[1] = SWAP_LONG(fpu->x10.x[1]);
  609.     fpu->x10.x[2] = SWAP_LONG(fpu->x10.x[2]);
  610.     fpu->x10.x[3] = SWAP_LONG(fpu->x10.x[3]);
  611.     fpu->x11.x[0] = SWAP_LONG(fpu->x11.x[0]);
  612.     fpu->x11.x[1] = SWAP_LONG(fpu->x11.x[1]);
  613.     fpu->x11.x[2] = SWAP_LONG(fpu->x11.x[2]);
  614.     fpu->x11.x[3] = SWAP_LONG(fpu->x11.x[3]);
  615.     fpu->x12.x[0] = SWAP_LONG(fpu->x12.x[0]);
  616.     fpu->x12.x[1] = SWAP_LONG(fpu->x12.x[1]);
  617.     fpu->x12.x[2] = SWAP_LONG(fpu->x12.x[2]);
  618.     fpu->x12.x[3] = SWAP_LONG(fpu->x12.x[3]);
  619.     fpu->x13.x[0] = SWAP_LONG(fpu->x13.x[0]);
  620.     fpu->x13.x[1] = SWAP_LONG(fpu->x13.x[1]);
  621.     fpu->x13.x[2] = SWAP_LONG(fpu->x13.x[2]);
  622.     fpu->x13.x[3] = SWAP_LONG(fpu->x13.x[3]);
  623.     fpu->x14.x[0] = SWAP_LONG(fpu->x14.x[0]);
  624.     fpu->x14.x[1] = SWAP_LONG(fpu->x14.x[1]);
  625.     fpu->x14.x[2] = SWAP_LONG(fpu->x14.x[2]);
  626.     fpu->x14.x[3] = SWAP_LONG(fpu->x14.x[3]);
  627.     fpu->x15.x[0] = SWAP_LONG(fpu->x15.x[0]);
  628.     fpu->x15.x[1] = SWAP_LONG(fpu->x15.x[1]);
  629.     fpu->x15.x[2] = SWAP_LONG(fpu->x15.x[2]);
  630.     fpu->x15.x[3] = SWAP_LONG(fpu->x15.x[3]);
  631.     fpu->x16.x[0] = SWAP_LONG(fpu->x16.x[0]);
  632.     fpu->x16.x[1] = SWAP_LONG(fpu->x16.x[1]);
  633.     fpu->x16.x[2] = SWAP_LONG(fpu->x16.x[2]);
  634.     fpu->x16.x[3] = SWAP_LONG(fpu->x16.x[3]);
  635.     fpu->x17.x[0] = SWAP_LONG(fpu->x17.x[0]);
  636.     fpu->x17.x[1] = SWAP_LONG(fpu->x17.x[1]);
  637.     fpu->x17.x[2] = SWAP_LONG(fpu->x17.x[2]);
  638.     fpu->x17.x[3] = SWAP_LONG(fpu->x17.x[3]);
  639.     fpu->x18.x[0] = SWAP_LONG(fpu->x18.x[0]);
  640.     fpu->x18.x[1] = SWAP_LONG(fpu->x18.x[1]);
  641.     fpu->x18.x[2] = SWAP_LONG(fpu->x18.x[2]);
  642.     fpu->x18.x[3] = SWAP_LONG(fpu->x18.x[3]);
  643.     fpu->x19.x[0] = SWAP_LONG(fpu->x19.x[0]);
  644.     fpu->x19.x[1] = SWAP_LONG(fpu->x19.x[1]);
  645.     fpu->x19.x[2] = SWAP_LONG(fpu->x19.x[2]);
  646.     fpu->x19.x[3] = SWAP_LONG(fpu->x19.x[3]);
  647.     fpu->x20.x[0] = SWAP_LONG(fpu->x20.x[0]);
  648.     fpu->x20.x[1] = SWAP_LONG(fpu->x20.x[1]);
  649.     fpu->x20.x[2] = SWAP_LONG(fpu->x20.x[2]);
  650.     fpu->x20.x[3] = SWAP_LONG(fpu->x20.x[3]);
  651.     fpu->x21.x[0] = SWAP_LONG(fpu->x21.x[0]);
  652.     fpu->x21.x[1] = SWAP_LONG(fpu->x21.x[1]);
  653.     fpu->x21.x[2] = SWAP_LONG(fpu->x21.x[2]);
  654.     fpu->x21.x[3] = SWAP_LONG(fpu->x21.x[3]);
  655.     fpu->x22.x[0] = SWAP_LONG(fpu->x22.x[0]);
  656.     fpu->x22.x[1] = SWAP_LONG(fpu->x22.x[1]);
  657.     fpu->x22.x[2] = SWAP_LONG(fpu->x22.x[2]);
  658.     fpu->x22.x[3] = SWAP_LONG(fpu->x22.x[3]);
  659.     fpu->x23.x[0] = SWAP_LONG(fpu->x23.x[0]);
  660.     fpu->x23.x[1] = SWAP_LONG(fpu->x23.x[1]);
  661.     fpu->x23.x[2] = SWAP_LONG(fpu->x23.x[2]);
  662.     fpu->x23.x[3] = SWAP_LONG(fpu->x23.x[3]);
  663.     fpu->x24.x[0] = SWAP_LONG(fpu->x24.x[0]);
  664.     fpu->x24.x[1] = SWAP_LONG(fpu->x24.x[1]);
  665.     fpu->x24.x[2] = SWAP_LONG(fpu->x24.x[2]);
  666.     fpu->x24.x[3] = SWAP_LONG(fpu->x24.x[3]);
  667.     fpu->x25.x[0] = SWAP_LONG(fpu->x25.x[0]);
  668.     fpu->x25.x[1] = SWAP_LONG(fpu->x25.x[1]);
  669.     fpu->x25.x[2] = SWAP_LONG(fpu->x25.x[2]);
  670.     fpu->x25.x[3] = SWAP_LONG(fpu->x25.x[3]);
  671.     fpu->x26.x[0] = SWAP_LONG(fpu->x26.x[0]);
  672.     fpu->x26.x[1] = SWAP_LONG(fpu->x26.x[1]);
  673.     fpu->x26.x[2] = SWAP_LONG(fpu->x26.x[2]);
  674.     fpu->x26.x[3] = SWAP_LONG(fpu->x26.x[3]);
  675.     fpu->x27.x[0] = SWAP_LONG(fpu->x27.x[0]);
  676.     fpu->x27.x[1] = SWAP_LONG(fpu->x27.x[1]);
  677.     fpu->x27.x[2] = SWAP_LONG(fpu->x27.x[2]);
  678.     fpu->x27.x[3] = SWAP_LONG(fpu->x27.x[3]);
  679.     fpu->x28.x[0] = SWAP_LONG(fpu->x28.x[0]);
  680.     fpu->x28.x[1] = SWAP_LONG(fpu->x28.x[1]);
  681.     fpu->x28.x[2] = SWAP_LONG(fpu->x28.x[2]);
  682.     fpu->x28.x[3] = SWAP_LONG(fpu->x28.x[3]);
  683.     fpu->x29.x[0] = SWAP_LONG(fpu->x29.x[0]);
  684.     fpu->x29.x[1] = SWAP_LONG(fpu->x29.x[1]);
  685.     fpu->x29.x[2] = SWAP_LONG(fpu->x29.x[2]);
  686.     fpu->x29.x[3] = SWAP_LONG(fpu->x29.x[3]);
  687.     fpu->x30.x[0] = SWAP_LONG(fpu->x30.x[0]);
  688.     fpu->x30.x[1] = SWAP_LONG(fpu->x30.x[1]);
  689.     fpu->x30.x[2] = SWAP_LONG(fpu->x30.x[2]);
  690.     fpu->x30.x[3] = SWAP_LONG(fpu->x30.x[3]);
  691.     fpu->x31.x[0] = SWAP_LONG(fpu->x31.x[0]);
  692.     fpu->x31.x[1] = SWAP_LONG(fpu->x31.x[1]);
  693.     fpu->x31.x[2] = SWAP_LONG(fpu->x31.x[2]);
  694.     fpu->x31.x[3] = SWAP_LONG(fpu->x31.x[3]);
  695.  
  696.     if(target_byte_sex == host_byte_sex){
  697.         memcpy(&ssr, &(fpu->fpsr), sizeof(struct swapped_m88k_fpsr));
  698.         ssr.u.word = SWAP_LONG(ssr.u.word);
  699.         fpu->fpsr.afinx = ssr.u.fields.afinx;
  700.         fpu->fpsr.afovf = ssr.u.fields.afovf;
  701.         fpu->fpsr.afunf = ssr.u.fields.afunf;
  702.         fpu->fpsr.afdvz = ssr.u.fields.afdvz;
  703.         fpu->fpsr.afinv = ssr.u.fields.afinv;
  704.         fpu->fpsr.xmod = ssr.u.fields.xmod;
  705.  
  706.         memcpy(&scr, &(fpu->fpcr), sizeof(struct swapped_m88k_fpcr));
  707.         scr.u.word = SWAP_LONG(scr.u.word);
  708.         fpu->fpcr.efinx = scr.u.fields.efinx;
  709.         fpu->fpcr.efovf = scr.u.fields.efovf;
  710.         fpu->fpcr.efunf = scr.u.fields.efunf;
  711.         fpu->fpcr.efdvz = scr.u.fields.efdvz;
  712.         fpu->fpcr.efinv = scr.u.fields.efinv;
  713.         fpu->fpcr.rm = scr.u.fields.rm;
  714.     }
  715.     else{
  716.         ssr.u.fields.afinx = fpu->fpsr.afinx;
  717.         ssr.u.fields.afovf = fpu->fpsr.afovf;
  718.         ssr.u.fields.afunf = fpu->fpsr.afunf;
  719.         ssr.u.fields.afdvz = fpu->fpsr.afdvz;
  720.         ssr.u.fields.afinv = fpu->fpsr.afinv;
  721.         ssr.u.fields.xmod = fpu->fpsr.xmod;
  722.         ssr.u.word = SWAP_LONG(ssr.u.word);
  723.         memcpy(&(fpu->fpsr), &ssr, sizeof(struct swapped_m88k_fpsr));
  724.  
  725.         scr.u.fields.efinx = fpu->fpcr.efinx;
  726.         scr.u.fields.efovf = fpu->fpcr.efovf;
  727.         scr.u.fields.efunf = fpu->fpcr.efunf;
  728.         scr.u.fields.efdvz = fpu->fpcr.efdvz;
  729.         scr.u.fields.efinv = fpu->fpcr.efinv;
  730.         scr.u.fields.rm = fpu->fpcr.rm;
  731.         scr.u.word = SWAP_LONG(scr.u.word);
  732.         memcpy(&(fpu->fpcr), &scr, sizeof(struct swapped_m88k_fpcr));
  733.     }
  734. }
  735.  
  736. void
  737. swap_m88k_thread_state_user_t(
  738. m88k_thread_state_user_t *user,
  739. enum byte_sex target_byte_sex)
  740. {
  741.     user->user = SWAP_LONG(user->user);
  742. }
  743.  
  744. void
  745. swap_m88110_thread_state_impl_t(
  746. m88110_thread_state_impl_t *spu,
  747. enum byte_sex target_byte_sex)
  748. {
  749.     unsigned long i;
  750.     enum byte_sex host_byte_sex;
  751.  
  752.     struct swapped_m88110_bp_ctrl {
  753.     union {
  754.         struct {
  755.         unsigned    v:BIT_WIDTH(0);
  756.         m88110_match_t    addr_match:BITS_WIDTH(12,1);
  757.         unsigned    :BITS_WIDTH(26,13);
  758.         unsigned    rwm:BIT_WIDTH(27);
  759.         unsigned    rw:BIT_WIDTH(28);
  760.         unsigned    :BITS_WIDTH(31,29);
  761.         } fields;
  762.         unsigned long word;
  763.     } u;
  764.     } sbpc;
  765.  
  766.     struct swap_m88110_psr {
  767.     union {
  768.         struct {
  769.         unsigned    :BITS_WIDTH(1,0);
  770.         unsigned    mxm_dis:BIT_WIDTH(2);
  771.         unsigned    sfu1dis:BIT_WIDTH(3);
  772.         unsigned    :BITS_WIDTH(22,4);
  773.         unsigned    trace:BIT_WIDTH(23);
  774.         unsigned    :BIT_WIDTH(24);
  775.         unsigned    sm:BIT_WIDTH(25);
  776.         unsigned    sgn_imd:BIT_WIDTH(26);
  777.         unsigned    :BIT_WIDTH(27);
  778.         unsigned    c:BIT_WIDTH(28);
  779.         unsigned    se:BIT_WIDTH(29);
  780.         unsigned    le:BIT_WIDTH(30);
  781.         unsigned    supr:BIT_WIDTH(31);
  782.         } fields;
  783.         unsigned long word;
  784.     } u;
  785.     } spsr;
  786.  
  787.     struct swapped_m88110_fp_trap_status {
  788.     union {
  789.         struct {
  790.         unsigned    efinx:BIT_WIDTH(0);
  791.         unsigned    efovf:BIT_WIDTH(1);
  792.         unsigned    efunf:BIT_WIDTH(2);
  793.         unsigned    efdvz:BIT_WIDTH(3);
  794.         unsigned    efinv:BIT_WIDTH(4);
  795.         unsigned    priv:BIT_WIDTH(5);
  796.         unsigned    unimp:BIT_WIDTH(6);
  797.         unsigned    int:BIT_WIDTH(7);
  798.         unsigned    sfu1_disabled:BIT_WIDTH(8);
  799.         unsigned    :BITS_WIDTH(13,9);
  800.         m88110_iresult_size_t    iresult_size:BITS_WIDTH(15,14);
  801.         unsigned    :BITS_WIDTH(31,16);
  802.         } fields;
  803.         unsigned long word;
  804.     } u;
  805.     } sfps;
  806.  
  807.     host_byte_sex = get_host_byte_sex();
  808.  
  809.     if(target_byte_sex == host_byte_sex){
  810.         for(i = 0; i < M88110_N_DATA_BP; i++){
  811.         spu->data_bp[i].addr = SWAP_LONG(spu->data_bp[i].addr);
  812.         memcpy(&sbpc, &(spu->data_bp[i].ctrl),
  813.                sizeof(struct swapped_m88110_bp_ctrl));
  814.         sbpc.u.word = SWAP_LONG(sbpc.u.word);
  815.         spu->data_bp[i].ctrl.v = sbpc.u.fields.v;
  816.         spu->data_bp[i].ctrl.addr_match = sbpc.u.fields.addr_match;
  817.         spu->data_bp[i].ctrl.rwm = sbpc.u.fields.rwm;
  818.         spu->data_bp[i].ctrl.rw = sbpc.u.fields.rw;
  819.         }
  820.  
  821.         memcpy(&spsr, &(spu->psr), sizeof(struct swap_m88110_psr));
  822.         spsr.u.word = SWAP_LONG(spsr.u.word);
  823.         spu->psr.mxm_dis = spsr.u.fields.mxm_dis;
  824.         spu->psr.sfu1dis = spsr.u.fields.sfu1dis;
  825.         spu->psr.trace = spsr.u.fields.trace;
  826.         spu->psr.sm = spsr.u.fields.sm;
  827.         spu->psr.sgn_imd = spsr.u.fields.sgn_imd;
  828.         spu->psr.c = spsr.u.fields.c;
  829.         spu->psr.se = spsr.u.fields.se;
  830.         spu->psr.le = spsr.u.fields.le;
  831.         spu->psr.supr = spsr.u.fields.supr;
  832.  
  833.         memcpy(&sfps, &(spu->fp_trap_status),
  834.            sizeof(struct swapped_m88110_fp_trap_status));
  835.         sfps.u.word = SWAP_LONG(sfps.u.word);
  836.         spu->fp_trap_status.efinx = sfps.u.fields.efinx;
  837.         spu->fp_trap_status.efovf = sfps.u.fields.efovf;
  838.         spu->fp_trap_status.efunf = sfps.u.fields.efunf;
  839.         spu->fp_trap_status.efdvz = sfps.u.fields.efdvz;
  840.         spu->fp_trap_status.efinv = sfps.u.fields.efinv;
  841.         spu->fp_trap_status.priv = sfps.u.fields.priv;
  842.         spu->fp_trap_status.unimp = sfps.u.fields.unimp;
  843.         spu->fp_trap_status.sfu1_disabled = sfps.u.fields.sfu1_disabled;
  844.         spu->fp_trap_status.iresult_size = sfps.u.fields.iresult_size;
  845.     }
  846.     else{
  847.         for(i = 0; i < M88110_N_DATA_BP; i++){
  848.         spu->data_bp[i].addr = SWAP_LONG(spu->data_bp[i].addr);
  849.         sbpc.u.fields.v = spu->data_bp[i].ctrl.v;
  850.         sbpc.u.fields.addr_match = spu->data_bp[i].ctrl.addr_match;
  851.         sbpc.u.fields.rwm = spu->data_bp[i].ctrl.rwm;
  852.         sbpc.u.fields.rw = spu->data_bp[i].ctrl.rw;
  853.         sbpc.u.word = SWAP_LONG(sbpc.u.word);
  854.         memcpy(&(spu->data_bp[i].ctrl), &sbpc,
  855.                sizeof(struct swapped_m88110_bp_ctrl));
  856.         }
  857.  
  858.         spsr.u.fields.mxm_dis = spu->psr.mxm_dis;
  859.         spsr.u.fields.sfu1dis = spu->psr.sfu1dis;
  860.         spsr.u.fields.trace = spu->psr.trace;
  861.         spsr.u.fields.sm = spu->psr.sm;
  862.         spsr.u.fields.sgn_imd = spu->psr.sgn_imd;
  863.         spsr.u.fields.c = spu->psr.c;
  864.         spsr.u.fields.se = spu->psr.se;
  865.         spsr.u.fields.le = spu->psr.le;
  866.         spsr.u.fields.supr = spu->psr.supr;
  867.         spsr.u.word = SWAP_LONG(spsr.u.word);
  868.         memcpy(&(spu->psr), &spsr, sizeof(struct swap_m88110_psr));
  869.  
  870.         sfps.u.fields.efinx = spu->fp_trap_status.efinx;
  871.         sfps.u.fields.efovf = spu->fp_trap_status.efovf;
  872.         sfps.u.fields.efunf = spu->fp_trap_status.efunf;
  873.         sfps.u.fields.efdvz = spu->fp_trap_status.efdvz;
  874.         sfps.u.fields.efinv = spu->fp_trap_status.efinv;
  875.         sfps.u.fields.priv = spu->fp_trap_status.priv;
  876.         sfps.u.fields.unimp = spu->fp_trap_status.unimp;
  877.         sfps.u.fields.sfu1_disabled = spu->fp_trap_status.sfu1_disabled;
  878.         sfps.u.fields.iresult_size = spu->fp_trap_status.iresult_size;
  879.         sfps.u.word = SWAP_LONG(sfps.u.word);
  880.         memcpy(&(spu->fp_trap_status), &sfps,
  881.            sizeof(struct swapped_m88110_fp_trap_status));
  882.     }
  883.     spu->intermediate_result.x[0] =
  884.         SWAP_LONG(spu->intermediate_result.x[0]);
  885.     spu->intermediate_result.x[1] =
  886.         SWAP_LONG(spu->intermediate_result.x[1]);
  887.     spu->intermediate_result.x[2] =
  888.         SWAP_LONG(spu->intermediate_result.x[2]);
  889.     spu->intermediate_result.x[3] =
  890.         SWAP_LONG(spu->intermediate_result.x[3]);
  891. }
  892.  
  893. void
  894. swap_i860_thread_state_regs(
  895. struct i860_thread_state_regs *cpu,
  896. enum byte_sex target_byte_sex)
  897. {
  898.     unsigned long i;
  899.  
  900.     for(i = 0; i < 31; i++)
  901.         cpu->ireg[i] = SWAP_LONG(cpu->ireg[i]);
  902.     for(i = 0; i < 30; i++)
  903.         cpu->freg[i] = SWAP_LONG(cpu->freg[i]);
  904.     cpu->psr = SWAP_LONG(cpu->psr);
  905.     cpu->epsr = SWAP_LONG(cpu->epsr);
  906.     cpu->db = SWAP_LONG(cpu->db);
  907.     cpu->pc = SWAP_LONG(cpu->pc);
  908.     cpu->_padding_ = SWAP_LONG(cpu->_padding_);
  909.     cpu->Mres3 = SWAP_DOUBLE(cpu->Mres3);
  910.     cpu->Ares3 = SWAP_DOUBLE(cpu->Ares3);
  911.     cpu->Mres2 = SWAP_DOUBLE(cpu->Mres2);
  912.     cpu->Ares2 = SWAP_DOUBLE(cpu->Ares2);
  913.     cpu->Mres1 = SWAP_DOUBLE(cpu->Mres1);
  914.     cpu->Ares1 = SWAP_DOUBLE(cpu->Ares1);
  915.     cpu->Ires1 = SWAP_DOUBLE(cpu->Ires1);
  916.     cpu->Lres3m = SWAP_DOUBLE(cpu->Lres3m);
  917.     cpu->Lres2m = SWAP_DOUBLE(cpu->Lres2m);
  918.     cpu->Lres1m = SWAP_DOUBLE(cpu->Lres1m);
  919.     cpu->KR = SWAP_DOUBLE(cpu->KR);
  920.     cpu->KI = SWAP_DOUBLE(cpu->KI);
  921.     cpu->T = SWAP_DOUBLE(cpu->T);
  922.     cpu->Fsr3 = SWAP_LONG(cpu->Fsr3);
  923.     cpu->Fsr2 = SWAP_LONG(cpu->Fsr2);
  924.     cpu->Fsr1 = SWAP_LONG(cpu->Fsr1);
  925.     cpu->Mergelo32 = SWAP_LONG(cpu->Mergelo32);
  926.     cpu->Mergehi32 = SWAP_LONG(cpu->Mergehi32);
  927. }
  928.  
  929. void
  930. swap_i386_thread_state(
  931. i386_thread_state_t *cpu,
  932. enum byte_sex target_byte_sex)
  933. {
  934.     cpu->eax = SWAP_LONG(cpu->eax);
  935.     cpu->ebx = SWAP_LONG(cpu->ebx);
  936.     cpu->ecx = SWAP_LONG(cpu->ecx);
  937.     cpu->edx = SWAP_LONG(cpu->edx);
  938.     cpu->edi = SWAP_LONG(cpu->edi);
  939.     cpu->esi = SWAP_LONG(cpu->esi);
  940.     cpu->ebp = SWAP_LONG(cpu->ebp);
  941.     cpu->esp = SWAP_LONG(cpu->esp);
  942.     cpu->ss = SWAP_LONG(cpu->ss);
  943.     cpu->eflags = SWAP_LONG(cpu->eflags);
  944.     cpu->eip = SWAP_LONG(cpu->eip);
  945.     cpu->cs = SWAP_LONG(cpu->cs);
  946.     cpu->ds = SWAP_LONG(cpu->ds);
  947.     cpu->es = SWAP_LONG(cpu->es);
  948.     cpu->fs = SWAP_LONG(cpu->fs);
  949.     cpu->gs = SWAP_LONG(cpu->gs);
  950. }
  951.  
  952. void
  953. swap_i386_thread_fpstate(
  954. i386_thread_fpstate_t *fpu,
  955. enum byte_sex target_byte_sex)
  956. {
  957.     struct swapped_fp_control {
  958.     union {
  959.         struct {
  960.         unsigned short
  961.                 :3,
  962.             /*inf*/ :1,
  963.             rc        :2,
  964.             pc        :2,
  965.                 :2,
  966.             precis  :1,
  967.             undfl   :1,
  968.             ovrfl   :1,
  969.             zdiv    :1,
  970.             denorm  :1,
  971.             invalid :1;
  972.         } fields;
  973.         unsigned short half;
  974.     } u;
  975.     } sfpc;
  976.  
  977.     struct swapped_fp_status {
  978.     union {
  979.         struct {
  980.         unsigned short
  981.             busy    :1,
  982.             c3        :1,
  983.             tos        :3,
  984.             c2        :1,
  985.             c1        :1,
  986.             c0        :1,
  987.             errsumm :1,
  988.             stkflt  :1,
  989.             precis  :1,
  990.             undfl   :1,
  991.             ovrfl   :1,
  992.             zdiv    :1,
  993.             denorm  :1,
  994.             invalid :1;
  995.         } fields;
  996.         unsigned short half;
  997.     } u;
  998.     } sfps;
  999.  
  1000.     struct swapped_fp_tag {
  1001.     union {
  1002.         struct {
  1003.         unsigned short
  1004.             tag7 :2,
  1005.             tag6 :2,
  1006.             tag5 :2,
  1007.             tag4 :2,
  1008.             tag3 :2,
  1009.             tag2 :2,
  1010.             tag1 :2,
  1011.             tag0 :2;
  1012.         } fields;
  1013.         unsigned short half;
  1014.     } u;
  1015.     } sfpt;
  1016.  
  1017.     struct swapped_fp_data_reg {
  1018.     unsigned short mant;
  1019.     unsigned short mant1 :16,
  1020.                mant2 :16,
  1021.                mant3 :16;
  1022.     union {
  1023.         struct {
  1024.         unsigned short sign :1,
  1025.                    exp  :15;
  1026.         } fields;
  1027.         unsigned short half;
  1028.     } u;
  1029.     } sfpd;
  1030.  
  1031.     struct swapped_sel {
  1032.     union {
  1033.         struct {
  1034.             unsigned short
  1035.             index :13,
  1036.             ti    :1,
  1037.             rpl   :2;
  1038.         } fields;
  1039.         unsigned short half;
  1040.     } u;
  1041.     } ss;
  1042.  
  1043.     enum byte_sex host_byte_sex;
  1044.     unsigned long i;
  1045.  
  1046.     host_byte_sex = get_host_byte_sex();
  1047.  
  1048.     fpu->environ.ip = SWAP_LONG(fpu->environ.ip);
  1049.     fpu->environ.opcode = SWAP_SHORT(fpu->environ.opcode);
  1050.     fpu->environ.dp = SWAP_LONG(fpu->environ.dp);
  1051.  
  1052.     if(target_byte_sex == host_byte_sex){
  1053.         memcpy(&sfpc, &(fpu->environ.control),
  1054.            sizeof(struct swapped_fp_control));
  1055.         sfpc.u.half = SWAP_SHORT(sfpc.u.half);
  1056.         fpu->environ.control.rc = sfpc.u.fields.rc;
  1057.         fpu->environ.control.pc = sfpc.u.fields.pc;
  1058.         fpu->environ.control.precis = sfpc.u.fields.precis;
  1059.         fpu->environ.control.undfl = sfpc.u.fields.undfl;
  1060.         fpu->environ.control.ovrfl = sfpc.u.fields.ovrfl;
  1061.         fpu->environ.control.zdiv = sfpc.u.fields.zdiv;
  1062.         fpu->environ.control.denorm = sfpc.u.fields.denorm;
  1063.         fpu->environ.control.invalid = sfpc.u.fields.invalid;
  1064.  
  1065.         memcpy(&sfps, &(fpu->environ.status),
  1066.            sizeof(struct swapped_fp_status));
  1067.         sfps.u.half = SWAP_SHORT(sfps.u.half);
  1068.         fpu->environ.status.busy = sfps.u.fields.busy;
  1069.         fpu->environ.status.c3 = sfps.u.fields.c3;
  1070.         fpu->environ.status.tos = sfps.u.fields.tos;
  1071.         fpu->environ.status.c2 = sfps.u.fields.c2;
  1072.         fpu->environ.status.c1 = sfps.u.fields.c1;
  1073.         fpu->environ.status.c0 = sfps.u.fields.c0;
  1074.         fpu->environ.status.errsumm = sfps.u.fields.errsumm;
  1075.         fpu->environ.status.stkflt = sfps.u.fields.stkflt;
  1076.         fpu->environ.status.precis = sfps.u.fields.precis;
  1077.         fpu->environ.status.undfl = sfps.u.fields.undfl;
  1078.         fpu->environ.status.ovrfl = sfps.u.fields.ovrfl;
  1079.         fpu->environ.status.zdiv = sfps.u.fields.zdiv;
  1080.         fpu->environ.status.denorm = sfps.u.fields.denorm;
  1081.         fpu->environ.status.invalid = sfps.u.fields.invalid;
  1082.  
  1083.         memcpy(&sfpt, &(fpu->environ.tag),
  1084.            sizeof(struct swapped_fp_tag));
  1085.         sfpt.u.half = SWAP_SHORT(sfpt.u.half);
  1086.         fpu->environ.tag.tag7 = sfpt.u.fields.tag7;
  1087.         fpu->environ.tag.tag6 = sfpt.u.fields.tag6;
  1088.         fpu->environ.tag.tag5 = sfpt.u.fields.tag5;
  1089.         fpu->environ.tag.tag4 = sfpt.u.fields.tag4;
  1090.         fpu->environ.tag.tag3 = sfpt.u.fields.tag3;
  1091.         fpu->environ.tag.tag2 = sfpt.u.fields.tag2;
  1092.         fpu->environ.tag.tag1 = sfpt.u.fields.tag1;
  1093.         fpu->environ.tag.tag0 = sfpt.u.fields.tag0;
  1094.  
  1095.         memcpy(&ss, &(fpu->environ.cs),
  1096.            sizeof(struct swapped_sel));
  1097.         ss.u.half = SWAP_SHORT(ss.u.half);
  1098.         fpu->environ.cs.index = ss.u.fields.index;
  1099.         fpu->environ.cs.ti = ss.u.fields.ti;
  1100.         fpu->environ.cs.rpl = ss.u.fields.rpl;
  1101.  
  1102.         memcpy(&ss, &(fpu->environ.ds),
  1103.            sizeof(struct swapped_sel));
  1104.         ss.u.half = SWAP_SHORT(ss.u.half);
  1105.         fpu->environ.ds.index = ss.u.fields.index;
  1106.         fpu->environ.ds.ti = ss.u.fields.ti;
  1107.         fpu->environ.ds.rpl = ss.u.fields.rpl;
  1108.     
  1109.         for(i = 0; i < 8; i++){
  1110.         memcpy(&sfpd, &(fpu->stack.ST[i]),
  1111.                sizeof(struct swapped_fp_data_reg));
  1112.         fpu->stack.ST[i].mant = SWAP_SHORT(sfpd.mant);
  1113.         fpu->stack.ST[i].mant1 = SWAP_SHORT(sfpd.mant1);
  1114.         fpu->stack.ST[i].mant2 = SWAP_SHORT(sfpd.mant2);
  1115.         fpu->stack.ST[i].mant3 = SWAP_SHORT(sfpd.mant3);
  1116.         sfpd.u.half = SWAP_SHORT(sfpd.u.half);
  1117.         fpu->stack.ST[i].exp = sfpd.u.fields.exp;
  1118.         fpu->stack.ST[i].sign = sfpd.u.fields.sign;
  1119.         }
  1120.     }
  1121.     else{
  1122.         sfpc.u.fields.rc = fpu->environ.control.rc;
  1123.         sfpc.u.fields.pc = fpu->environ.control.pc;
  1124.         sfpc.u.fields.precis = fpu->environ.control.precis;
  1125.         sfpc.u.fields.undfl = fpu->environ.control.undfl;
  1126.         sfpc.u.fields.ovrfl = fpu->environ.control.ovrfl;
  1127.         sfpc.u.fields.zdiv = fpu->environ.control.zdiv;
  1128.         sfpc.u.fields.denorm = fpu->environ.control.denorm;
  1129.         sfpc.u.fields.invalid = fpu->environ.control.invalid;
  1130.         sfpc.u.half = SWAP_SHORT(sfpc.u.half);
  1131.         memcpy(&(fpu->environ.control), &sfpc,
  1132.            sizeof(struct swapped_fp_control));
  1133.  
  1134.         sfps.u.fields.busy = fpu->environ.status.busy;
  1135.         sfps.u.fields.c3 = fpu->environ.status.c3;
  1136.         sfps.u.fields.tos = fpu->environ.status.tos;
  1137.         sfps.u.fields.c2 = fpu->environ.status.c2;
  1138.         sfps.u.fields.c1 = fpu->environ.status.c1;
  1139.         sfps.u.fields.c0 = fpu->environ.status.c0;
  1140.         sfps.u.fields.errsumm = fpu->environ.status.errsumm;
  1141.         sfps.u.fields.stkflt = fpu->environ.status.stkflt;
  1142.         sfps.u.fields.precis = fpu->environ.status.precis;
  1143.         sfps.u.fields.undfl = fpu->environ.status.undfl;
  1144.         sfps.u.fields.ovrfl = fpu->environ.status.ovrfl;
  1145.         sfps.u.fields.zdiv = fpu->environ.status.zdiv;
  1146.         sfps.u.fields.denorm = fpu->environ.status.denorm;
  1147.         sfps.u.fields.invalid = fpu->environ.status.invalid;
  1148.         sfps.u.half = SWAP_SHORT(sfps.u.half);
  1149.         memcpy(&(fpu->environ.status), &sfps,
  1150.            sizeof(struct swapped_fp_status));
  1151.  
  1152.         sfpt.u.fields.tag7 = fpu->environ.tag.tag7;
  1153.         sfpt.u.fields.tag6 = fpu->environ.tag.tag6;
  1154.         sfpt.u.fields.tag5 = fpu->environ.tag.tag5;
  1155.         sfpt.u.fields.tag4 = fpu->environ.tag.tag4;
  1156.         sfpt.u.fields.tag3 = fpu->environ.tag.tag3;
  1157.         sfpt.u.fields.tag2 = fpu->environ.tag.tag2;
  1158.         sfpt.u.fields.tag1 = fpu->environ.tag.tag1;
  1159.         sfpt.u.fields.tag0 = fpu->environ.tag.tag0;
  1160.         sfpt.u.half = SWAP_SHORT(sfpt.u.half);
  1161.         memcpy(&(fpu->environ.tag), &sfpt,
  1162.            sizeof(struct swapped_fp_tag));
  1163.  
  1164.         ss.u.fields.index = fpu->environ.cs.index;
  1165.         ss.u.fields.ti = fpu->environ.cs.ti;
  1166.         ss.u.fields.rpl = fpu->environ.cs.rpl;
  1167.         ss.u.half = SWAP_SHORT(ss.u.half);
  1168.         memcpy(&(fpu->environ.cs), &ss,
  1169.            sizeof(struct swapped_sel));
  1170.  
  1171.         ss.u.fields.index = fpu->environ.ds.index;
  1172.         ss.u.fields.ti = fpu->environ.ds.ti;
  1173.         ss.u.fields.rpl = fpu->environ.ds.rpl;
  1174.         ss.u.half = SWAP_SHORT(ss.u.half);
  1175.         memcpy(&(fpu->environ.cs), &ss,
  1176.            sizeof(struct swapped_sel));
  1177.  
  1178.         for(i = 0; i < 8; i++){
  1179.         sfpd.mant = SWAP_SHORT(fpu->stack.ST[i].mant);
  1180.         sfpd.mant1 = SWAP_SHORT(fpu->stack.ST[i].mant1);
  1181.         sfpd.mant2 = SWAP_SHORT(fpu->stack.ST[i].mant2);
  1182.         sfpd.mant3 = SWAP_SHORT(fpu->stack.ST[i].mant3);
  1183.         sfpd.u.fields.exp = fpu->stack.ST[i].exp;
  1184.         sfpd.u.fields.sign = fpu->stack.ST[i].sign;
  1185.         sfpd.u.half = SWAP_SHORT(sfpd.u.half);
  1186.         memcpy(&(fpu->stack.ST[i]), &sfpd,
  1187.                sizeof(struct swapped_fp_data_reg));
  1188.         }
  1189.     }
  1190. }
  1191.  
  1192. void
  1193. swap_i386_thread_exceptstate(
  1194. i386_thread_exceptstate_t *exc,
  1195. enum byte_sex target_byte_sex)
  1196. {
  1197.     struct swapped_err_code {
  1198.     union {
  1199.         struct err_code_normal {
  1200.         unsigned int        :16,
  1201.                 index    :13,
  1202.                 tbl    :2,
  1203.                 ext    :1;
  1204.         } normal;
  1205.         struct err_code_pgfault {
  1206.         unsigned int        :29,
  1207.                 user    :1,
  1208.                 wrtflt    :1,
  1209.                 prot    :1;
  1210.         } pgfault;
  1211.         unsigned long word;
  1212.     } u;
  1213.     } sec;
  1214.     unsigned long word;
  1215.     enum byte_sex host_byte_sex;
  1216.  
  1217.     host_byte_sex = get_host_byte_sex();
  1218.  
  1219.     exc->trapno = SWAP_LONG(exc->trapno);
  1220.     if(exc->trapno == 14){
  1221.         if(target_byte_sex == host_byte_sex){
  1222.         memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
  1223.         sec.u.word = SWAP_LONG(sec.u.word);
  1224.         exc->err.pgfault.user   = sec.u.pgfault.user;
  1225.         exc->err.pgfault.wrtflt = sec.u.pgfault.wrtflt;
  1226.         exc->err.pgfault.prot   = sec.u.pgfault.prot;
  1227.         }
  1228.         else{
  1229.         sec.u.pgfault.prot   = exc->err.pgfault.prot;
  1230.         sec.u.pgfault.wrtflt = exc->err.pgfault.wrtflt;
  1231.         sec.u.pgfault.user   = exc->err.pgfault.user;
  1232.         sec.u.word = SWAP_LONG(sec.u.word);
  1233.         memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
  1234.         }
  1235.     }
  1236.     else{
  1237.         if(target_byte_sex == host_byte_sex){
  1238.         memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code));
  1239.         sec.u.word = SWAP_LONG(sec.u.word);
  1240.         word = sec.u.normal.index;
  1241.         exc->err.normal.index = SWAP_LONG(word);
  1242.         exc->err.normal.tbl   = sec.u.normal.tbl;
  1243.         exc->err.normal.ext   = sec.u.normal.ext;
  1244.         }
  1245.         else{
  1246.         sec.u.normal.ext   = exc->err.normal.ext;
  1247.         sec.u.normal.tbl   = exc->err.normal.tbl;
  1248.         word = exc->err.normal.index;
  1249.         sec.u.normal.index = SWAP_LONG(word);
  1250.         sec.u.word = SWAP_LONG(sec.u.word);
  1251.         memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code));
  1252.         }
  1253.     }
  1254. }
  1255.  
  1256. void
  1257. swap_i386_thread_cthreadstate(
  1258. i386_thread_cthreadstate_t *user,
  1259. enum byte_sex target_byte_sex)
  1260. {
  1261.     user->self = SWAP_LONG(user->self);
  1262. }
  1263.  
  1264. void
  1265. swap_hppa_integer_thread_state(
  1266. struct hp_pa_integer_thread_state *regs,
  1267. enum byte_sex target_byte_order)
  1268. {
  1269.     regs->ts_gr1 = SWAP_LONG(regs->ts_gr1);
  1270.     regs->ts_gr2 = SWAP_LONG(regs->ts_gr2);
  1271.     regs->ts_gr3 = SWAP_LONG(regs->ts_gr3);
  1272.     regs->ts_gr4 = SWAP_LONG(regs->ts_gr4);
  1273.     regs->ts_gr5 = SWAP_LONG(regs->ts_gr5);
  1274.     regs->ts_gr6 = SWAP_LONG(regs->ts_gr6);
  1275.     regs->ts_gr7 = SWAP_LONG(regs->ts_gr7);
  1276.     regs->ts_gr8 = SWAP_LONG(regs->ts_gr8);
  1277.     regs->ts_gr9 = SWAP_LONG(regs->ts_gr9);
  1278.     regs->ts_gr10 = SWAP_LONG(regs->ts_gr10);
  1279.     regs->ts_gr11 = SWAP_LONG(regs->ts_gr11);
  1280.     regs->ts_gr12 = SWAP_LONG(regs->ts_gr12);
  1281.     regs->ts_gr13 = SWAP_LONG(regs->ts_gr13);
  1282.     regs->ts_gr14 = SWAP_LONG(regs->ts_gr14);
  1283.     regs->ts_gr15 = SWAP_LONG(regs->ts_gr15);
  1284.     regs->ts_gr16 = SWAP_LONG(regs->ts_gr16);
  1285.     regs->ts_gr17 = SWAP_LONG(regs->ts_gr17);
  1286.     regs->ts_gr18 = SWAP_LONG(regs->ts_gr18);
  1287.     regs->ts_gr19 = SWAP_LONG(regs->ts_gr19);
  1288.     regs->ts_gr20 = SWAP_LONG(regs->ts_gr20);
  1289.     regs->ts_gr21 = SWAP_LONG(regs->ts_gr21);
  1290.     regs->ts_gr22 = SWAP_LONG(regs->ts_gr22);
  1291.     regs->ts_gr23 = SWAP_LONG(regs->ts_gr23);
  1292.     regs->ts_gr24 = SWAP_LONG(regs->ts_gr24);
  1293.     regs->ts_gr25 = SWAP_LONG(regs->ts_gr25);
  1294.     regs->ts_gr26 = SWAP_LONG(regs->ts_gr26);
  1295.     regs->ts_gr27 = SWAP_LONG(regs->ts_gr27);
  1296.     regs->ts_gr28 = SWAP_LONG(regs->ts_gr28);
  1297.     regs->ts_gr29 = SWAP_LONG(regs->ts_gr29);
  1298.     regs->ts_gr30 = SWAP_LONG(regs->ts_gr30);
  1299.     regs->ts_gr31 = SWAP_LONG(regs->ts_gr31);
  1300.     regs->ts_sr0 = SWAP_LONG(regs->ts_sr0);
  1301.     regs->ts_sr1 = SWAP_LONG(regs->ts_sr1);
  1302.     regs->ts_sr2 = SWAP_LONG(regs->ts_sr2);
  1303.     regs->ts_sr3 = SWAP_LONG(regs->ts_sr3);
  1304.     regs->ts_sar = SWAP_LONG(regs->ts_sar);
  1305. }
  1306.  
  1307. void swap_hppa_frame_thread_state(
  1308.   struct hp_pa_frame_thread_state *frame,
  1309.   enum byte_sex target_byte_order)
  1310. {
  1311.   frame->ts_pcsq_front = SWAP_LONG(frame->ts_pcsq_front);
  1312.   frame->ts_pcsq_back = SWAP_LONG(frame->ts_pcsq_back);
  1313.   frame->ts_pcoq_front = SWAP_LONG(frame->ts_pcoq_front);
  1314.   frame->ts_pcoq_back = SWAP_LONG(frame->ts_pcoq_back);
  1315.   frame->ts_psw = SWAP_LONG(frame->ts_psw);
  1316.   frame->ts_unaligned_faults = SWAP_LONG(frame->ts_unaligned_faults);
  1317.   frame->ts_fault_address = SWAP_LONG(frame->ts_fault_address);
  1318.   frame->ts_step_range_start = SWAP_LONG(frame->ts_step_range_start);
  1319.   frame->ts_step_range_stop = SWAP_LONG(frame->ts_step_range_stop);
  1320. }
  1321.  
  1322. void swap_hppa_fp_thread_state(
  1323.   struct hp_pa_fp_thread_state *fp,
  1324.   enum byte_sex target_byte_order)
  1325. {
  1326.   fp->ts_fp0 = SWAP_DOUBLE(fp->ts_fp0);
  1327.   fp->ts_fp1 = SWAP_DOUBLE(fp->ts_fp1);
  1328.   fp->ts_fp2 = SWAP_DOUBLE(fp->ts_fp2);
  1329.   fp->ts_fp3 = SWAP_DOUBLE(fp->ts_fp3);
  1330.   fp->ts_fp4 = SWAP_DOUBLE(fp->ts_fp4);
  1331.   fp->ts_fp5 = SWAP_DOUBLE(fp->ts_fp5);
  1332.   fp->ts_fp6 = SWAP_DOUBLE(fp->ts_fp6);
  1333.   fp->ts_fp7 = SWAP_DOUBLE(fp->ts_fp7);
  1334.   fp->ts_fp8 = SWAP_DOUBLE(fp->ts_fp8);
  1335.   fp->ts_fp9 = SWAP_DOUBLE(fp->ts_fp9);
  1336.   fp->ts_fp10 = SWAP_DOUBLE(fp->ts_fp10);
  1337.   fp->ts_fp11 = SWAP_DOUBLE(fp->ts_fp11);
  1338.   fp->ts_fp12 = SWAP_DOUBLE(fp->ts_fp12);
  1339.   fp->ts_fp13 = SWAP_DOUBLE(fp->ts_fp13);
  1340.   fp->ts_fp14 = SWAP_DOUBLE(fp->ts_fp14);
  1341.   fp->ts_fp15 = SWAP_DOUBLE(fp->ts_fp15);
  1342.   fp->ts_fp16 = SWAP_DOUBLE(fp->ts_fp16);
  1343.   fp->ts_fp17 = SWAP_DOUBLE(fp->ts_fp17);
  1344.   fp->ts_fp18 = SWAP_DOUBLE(fp->ts_fp18);
  1345.   fp->ts_fp19 = SWAP_DOUBLE(fp->ts_fp19);
  1346.   fp->ts_fp20 = SWAP_DOUBLE(fp->ts_fp20);
  1347.   fp->ts_fp21 = SWAP_DOUBLE(fp->ts_fp21);
  1348.   fp->ts_fp22 = SWAP_DOUBLE(fp->ts_fp22);
  1349.   fp->ts_fp23 = SWAP_DOUBLE(fp->ts_fp23);
  1350.   fp->ts_fp24 = SWAP_DOUBLE(fp->ts_fp24);
  1351.   fp->ts_fp25 = SWAP_DOUBLE(fp->ts_fp25);
  1352.   fp->ts_fp26 = SWAP_DOUBLE(fp->ts_fp26);
  1353.   fp->ts_fp27 = SWAP_DOUBLE(fp->ts_fp27);
  1354.   fp->ts_fp28 = SWAP_DOUBLE(fp->ts_fp28);
  1355.   fp->ts_fp29 = SWAP_DOUBLE(fp->ts_fp29);
  1356.   fp->ts_fp30 = SWAP_DOUBLE(fp->ts_fp30);
  1357.   fp->ts_fp31 = SWAP_DOUBLE(fp->ts_fp31);
  1358. }
  1359.  
  1360. void
  1361. swap_sparc_thread_state_regs(
  1362.             struct sparc_thread_state_regs *cpu,
  1363.             enum byte_sex target_byte_sex)
  1364. {
  1365.   enum byte_sex host_byte_sex = get_host_byte_sex();
  1366.   struct p_status *pr_status = (struct p_status *) &(cpu->regs.r_psr);
  1367.  
  1368.   struct swapped_psr {
  1369.     union {
  1370.       struct {
  1371.     unsigned int 
  1372.     cwp:BITS_WIDTH(4,0),
  1373.     et:BIT_WIDTH(5),
  1374.     ps:BIT_WIDTH(6),
  1375.     s:BIT_WIDTH(7),
  1376.     pil:BITS_WIDTH(11,8),
  1377.     ef:BIT_WIDTH(12),
  1378.     ec:BIT_WIDTH(13),
  1379.     reserved:BITS_WIDTH(19,14),
  1380.     icc:BITS_WIDTH(23,20),
  1381.     ver:BITS_WIDTH(27,24),
  1382.     impl:BITS_WIDTH(31,28);
  1383.       } fields;
  1384.       unsigned int word;
  1385.     } u;
  1386.   } spsr;
  1387.  
  1388.   cpu->regs.r_pc = SWAP_LONG(cpu->regs.r_pc);
  1389.   cpu->regs.r_npc = SWAP_LONG(cpu->regs.r_npc);
  1390.   cpu->regs.r_y = SWAP_LONG(cpu->regs.r_y);
  1391.   cpu->regs.r_g1 = SWAP_LONG(cpu->regs.r_g1);
  1392.   cpu->regs.r_g2 = SWAP_LONG(cpu->regs.r_g2);
  1393.   cpu->regs.r_g3 = SWAP_LONG(cpu->regs.r_g3);
  1394.   cpu->regs.r_g4 = SWAP_LONG(cpu->regs.r_g4);
  1395.   cpu->regs.r_g5 = SWAP_LONG(cpu->regs.r_g5);
  1396.   cpu->regs.r_g6 = SWAP_LONG(cpu->regs.r_g6);
  1397.   cpu->regs.r_g7 = SWAP_LONG(cpu->regs.r_g7);
  1398.   cpu->regs.r_o0 = SWAP_LONG(cpu->regs.r_o0);
  1399.   cpu->regs.r_o1 = SWAP_LONG(cpu->regs.r_o1);
  1400.   cpu->regs.r_o2 = SWAP_LONG(cpu->regs.r_o2);
  1401.   cpu->regs.r_o3 = SWAP_LONG(cpu->regs.r_o3);
  1402.   cpu->regs.r_o4 = SWAP_LONG(cpu->regs.r_o4);
  1403.   cpu->regs.r_o5 = SWAP_LONG(cpu->regs.r_o5);
  1404.   cpu->regs.r_o6 = SWAP_LONG(cpu->regs.r_o6);
  1405.   cpu->regs.r_o7 = SWAP_LONG(cpu->regs.r_o7);
  1406.  
  1407.   if (target_byte_sex == host_byte_sex) {
  1408.     memcpy(&spsr, &(cpu->regs.r_psr), sizeof(struct swapped_psr));
  1409.     spsr.u.word = SWAP_LONG(spsr.u.word);
  1410.     pr_status->PSRREG.psr_bits.cwp = spsr.u.fields.cwp;
  1411.     pr_status->PSRREG.psr_bits.ps = spsr.u.fields.ps;
  1412.     pr_status->PSRREG.psr_bits.s = spsr.u.fields.s;
  1413.     pr_status->PSRREG.psr_bits.pil = spsr.u.fields.pil;
  1414.     pr_status->PSRREG.psr_bits.ef = spsr.u.fields.ef;
  1415.     pr_status->PSRREG.psr_bits.ec = spsr.u.fields.ec;
  1416.     pr_status->PSRREG.psr_bits.reserved = spsr.u.fields.reserved;
  1417.     pr_status->PSRREG.psr_bits.icc = spsr.u.fields.icc;
  1418.     pr_status->PSRREG.psr_bits.et = spsr.u.fields.ver;
  1419.     pr_status->PSRREG.psr_bits.impl = spsr.u.fields.impl;
  1420.   } else {
  1421.     spsr.u.fields.cwp = pr_status->PSRREG.psr_bits.cwp;
  1422.     spsr.u.fields.ps = pr_status->PSRREG.psr_bits.ps;
  1423.     spsr.u.fields.s = pr_status->PSRREG.psr_bits.s;
  1424.     spsr.u.fields.pil = pr_status->PSRREG.psr_bits.pil;
  1425.     spsr.u.fields.ef = pr_status->PSRREG.psr_bits.ef;
  1426.     spsr.u.fields.ec = pr_status->PSRREG.psr_bits.ec;
  1427.     spsr.u.fields.reserved = pr_status->PSRREG.psr_bits.reserved;
  1428.     spsr.u.fields.icc = pr_status->PSRREG.psr_bits.icc;
  1429.     spsr.u.fields.ver = pr_status->PSRREG.psr_bits.et;
  1430.     spsr.u.fields.impl = pr_status->PSRREG.psr_bits.impl;
  1431.     spsr.u.word = SWAP_LONG(spsr.u.word);
  1432.     memcpy(&(cpu->regs.r_psr), &spsr, sizeof(struct swapped_psr));
  1433.   }
  1434. }
  1435.  
  1436. void
  1437. swap_sparc_thread_state_fpu(
  1438.             struct sparc_thread_state_fpu *fpu,
  1439.             enum byte_sex target_byte_sex)
  1440. {
  1441.   enum byte_sex host_byte_sex = get_host_byte_sex();
  1442.   int i;
  1443.   struct f_status *fpu_status = (struct f_status *) &(fpu->fpu.Fpu_fsr);
  1444.  
  1445.   struct swapped_fsr {
  1446.     union {
  1447.       struct {
  1448.     unsigned int
  1449.     cexc:BITS_WIDTH(4,0),
  1450.     aexc:BITS_WIDTH(9,5),
  1451.     fcc:BITS_WIDTH(11,10),
  1452.     pr:BIT_WIDTH(12),
  1453.     qne:BIT_WIDTH(13),
  1454.     ftt:BITS_WIDTH(16,14),
  1455.     res:BITS_WIDTH(22,17),
  1456.     tem:BITS_WIDTH(27,23),
  1457.     rp:BITS_WIDTH(29,28),
  1458.     rd:BITS_WIDTH(31,30);
  1459.       } fields;
  1460.       unsigned int word;
  1461.     } u;
  1462.   } sfsr;
  1463.  
  1464.     
  1465.   /* floating point registers */
  1466.   for (i=0; i<16; i++)        /* 16 doubles */
  1467.     fpu->fpu.fpu_fr.Fpu_dregs[i] = SWAP_DOUBLE(fpu->fpu.fpu_fr.Fpu_dregs[i]);
  1468.   fpu->fpu.Fpu_q[0].FQu.whole = SWAP_DOUBLE(fpu->fpu.Fpu_q[0].FQu.whole);
  1469.   fpu->fpu.Fpu_q[1].FQu.whole = SWAP_DOUBLE(fpu->fpu.Fpu_q[1].FQu.whole);
  1470.   fpu->fpu.Fpu_flags = SWAP_LONG(fpu->fpu.Fpu_flags);
  1471.   fpu->fpu.Fpu_extra = SWAP_LONG(fpu->fpu.Fpu_extra);
  1472.   fpu->fpu.Fpu_qcnt = SWAP_LONG(fpu->fpu.Fpu_qcnt);
  1473.  
  1474.   if (target_byte_sex == host_byte_sex) {
  1475.     memcpy(&sfsr, &(fpu->fpu.Fpu_fsr), sizeof(unsigned int));
  1476.     sfsr.u.word = SWAP_LONG(sfsr.u.word);
  1477.     fpu_status->FPUREG.Fpu_fsr_bits.rd = sfsr.u.fields.rd;
  1478.     fpu_status->FPUREG.Fpu_fsr_bits.rp = sfsr.u.fields.rp;
  1479.     fpu_status->FPUREG.Fpu_fsr_bits.tem = sfsr.u.fields.tem;
  1480.     fpu_status->FPUREG.Fpu_fsr_bits.res = sfsr.u.fields.res;
  1481.     fpu_status->FPUREG.Fpu_fsr_bits.ftt = sfsr.u.fields.ftt;
  1482.     fpu_status->FPUREG.Fpu_fsr_bits.qne = sfsr.u.fields.qne;
  1483.     fpu_status->FPUREG.Fpu_fsr_bits.pr = sfsr.u.fields.pr;
  1484.     fpu_status->FPUREG.Fpu_fsr_bits.fcc = sfsr.u.fields.fcc;
  1485.     fpu_status->FPUREG.Fpu_fsr_bits.aexc = sfsr.u.fields.aexc;
  1486.     fpu_status->FPUREG.Fpu_fsr_bits.cexc = sfsr.u.fields.cexc;
  1487.   } else {
  1488.     sfsr.u.fields.rd = fpu_status->FPUREG.Fpu_fsr_bits.rd;
  1489.     sfsr.u.fields.rp = fpu_status->FPUREG.Fpu_fsr_bits.rp;
  1490.     sfsr.u.fields.tem = fpu_status->FPUREG.Fpu_fsr_bits.tem;
  1491.     sfsr.u.fields.res = fpu_status->FPUREG.Fpu_fsr_bits.res;
  1492.     sfsr.u.fields.ftt = fpu_status->FPUREG.Fpu_fsr_bits.ftt;
  1493.     sfsr.u.fields.qne = fpu_status->FPUREG.Fpu_fsr_bits.qne;
  1494.     sfsr.u.fields.pr = fpu_status->FPUREG.Fpu_fsr_bits.pr;
  1495.     sfsr.u.fields.fcc = fpu_status->FPUREG.Fpu_fsr_bits.fcc;
  1496.     sfsr.u.fields.aexc = fpu_status->FPUREG.Fpu_fsr_bits.aexc;
  1497.     sfsr.u.fields.cexc = fpu_status->FPUREG.Fpu_fsr_bits.cexc;
  1498.     sfsr.u.word = SWAP_LONG(sfsr.u.word);
  1499.     memcpy(&(fpu->fpu.Fpu_fsr), &sfsr, sizeof(struct swapped_fsr));
  1500.   }
  1501. }
  1502.  
  1503. void
  1504. swap_ident_command(
  1505. struct ident_command *id_cmd,
  1506. enum byte_sex target_byte_sex)
  1507. {
  1508.     id_cmd->cmd = SWAP_LONG(id_cmd->cmd);
  1509.     id_cmd->cmdsize = SWAP_LONG(id_cmd->cmdsize);
  1510. }
  1511.  
  1512. void
  1513. swap_nlist(
  1514. struct nlist *symbols,
  1515. unsigned long nsymbols,
  1516. enum byte_sex target_byte_sex)
  1517. {
  1518.     unsigned long i;
  1519.  
  1520.     for(i = 0; i < nsymbols; i++){
  1521.         symbols[i].n_un.n_strx = SWAP_LONG(symbols[i].n_un.n_strx);
  1522.         /* n_type */
  1523.         /* n_sect */
  1524.         symbols[i].n_desc = SWAP_SHORT(symbols[i].n_desc);
  1525.         symbols[i].n_value = SWAP_LONG(symbols[i].n_value);
  1526.     }
  1527. }
  1528.  
  1529. void
  1530. swap_ranlib(
  1531. struct ranlib *ranlibs,
  1532. unsigned long nranlibs,
  1533. enum byte_sex target_byte_sex)
  1534. {
  1535.     unsigned long i;
  1536.  
  1537.     for(i = 0; i < nranlibs; i++){
  1538.         ranlibs[i].ran_un.ran_strx = SWAP_LONG(ranlibs[i].ran_un.ran_strx);
  1539.         ranlibs[i].ran_off = SWAP_LONG(ranlibs[i].ran_off);
  1540.     }
  1541. }
  1542.  
  1543. void
  1544. swap_relocation_info(
  1545. struct relocation_info *relocs,
  1546. unsigned long nrelocs,
  1547. enum byte_sex target_byte_sex)
  1548. {
  1549.     unsigned long i;
  1550.     enum byte_sex host_byte_sex;
  1551.     enum bool to_host_byte_sex, scattered;
  1552.  
  1553.     struct swapped_relocation_info {
  1554.     long    r_address;
  1555.     union {
  1556.         struct {
  1557.         unsigned int
  1558.             r_type:4,
  1559.             r_extern:1,
  1560.             r_length:2,
  1561.             r_pcrel:1,
  1562.             r_symbolnum:24;
  1563.         } fields;
  1564.         unsigned long word;
  1565.     } u;
  1566.     } sr;
  1567.  
  1568.     struct swapped_scattered_relocation_info {
  1569.     unsigned long word;
  1570.     long    r_value;
  1571.     } *ssr;
  1572.  
  1573.     host_byte_sex = get_host_byte_sex();
  1574.     to_host_byte_sex = target_byte_sex == host_byte_sex;
  1575.  
  1576.     for(i = 0; i < nrelocs; i++){
  1577.         if(to_host_byte_sex)
  1578.         scattered = (SWAP_LONG(relocs[i].r_address) & R_SCATTERED) != 0;
  1579.         else
  1580.         scattered = ((relocs[i].r_address) & R_SCATTERED) != 0;
  1581.         if(scattered == FALSE){
  1582.         if(to_host_byte_sex){
  1583.             memcpy(&sr, relocs + i, sizeof(struct relocation_info));
  1584.             sr.r_address = SWAP_LONG(sr.r_address);
  1585.             sr.u.word = SWAP_LONG(sr.u.word);
  1586.             relocs[i].r_address = sr.r_address;
  1587.             relocs[i].r_symbolnum = sr.u.fields.r_symbolnum;
  1588.             relocs[i].r_pcrel = sr.u.fields.r_pcrel;
  1589.             relocs[i].r_length = sr.u.fields.r_length;
  1590.             relocs[i].r_extern = sr.u.fields.r_extern;
  1591.             relocs[i].r_type = sr.u.fields.r_type;
  1592.         }
  1593.         else{
  1594.             sr.r_address = relocs[i].r_address;
  1595.             sr.u.fields.r_symbolnum = relocs[i].r_symbolnum;
  1596.             sr.u.fields.r_length = relocs[i].r_length;
  1597.             sr.u.fields.r_pcrel = relocs[i].r_pcrel;
  1598.             sr.u.fields.r_extern = relocs[i].r_extern;
  1599.             sr.u.fields.r_type = relocs[i].r_type;
  1600.             sr.r_address = SWAP_LONG(sr.r_address);
  1601.             sr.u.word = SWAP_LONG(sr.u.word);
  1602.             memcpy(relocs + i, &sr, sizeof(struct relocation_info));
  1603.         }
  1604.         }
  1605.         else{
  1606.         ssr = (struct swapped_scattered_relocation_info *)(relocs + i);
  1607.         ssr->word = SWAP_LONG(ssr->word);
  1608.         ssr->r_value = SWAP_LONG(ssr->r_value);
  1609.         }
  1610.     }
  1611. }
  1612.  
  1613. void
  1614. swap_indirect_symbols(
  1615. unsigned long *indirect_symbols,
  1616. unsigned long nindirect_symbols,
  1617. enum byte_sex target_byte_sex)
  1618. {
  1619.     unsigned long i;
  1620.  
  1621.     for(i = 0; i < nindirect_symbols; i++)
  1622.         indirect_symbols[i] = SWAP_LONG(indirect_symbols[i]);
  1623. }
  1624.  
  1625. void
  1626. swap_dylib_reference(
  1627. struct dylib_reference *refs,
  1628. unsigned long nrefs,
  1629. enum byte_sex target_byte_sex)
  1630. {
  1631.     struct swapped_dylib_reference {
  1632.     union {
  1633.         struct {
  1634.         unsigned long
  1635.             flags:8,
  1636.             isym:24;
  1637.         } fields;
  1638.         unsigned long word;
  1639.     } u;
  1640.     } sref;
  1641.  
  1642.     unsigned long i;
  1643.     enum byte_sex host_byte_sex;
  1644.  
  1645.     host_byte_sex = get_host_byte_sex();
  1646.  
  1647.     for(i = 0; i < nrefs; i++){
  1648.         if(target_byte_sex == host_byte_sex){
  1649.         memcpy(&sref, refs + i, sizeof(struct swapped_dylib_reference));
  1650.         sref.u.word = SWAP_LONG(sref.u.word);
  1651.         refs[i].flags = sref.u.fields.flags;
  1652.         refs[i].isym = sref.u.fields.isym;
  1653.         }
  1654.         else{
  1655.         sref.u.fields.isym = refs[i].isym;
  1656.         sref.u.fields.flags = refs[i].flags;
  1657.         sref.u.word = SWAP_LONG(sref.u.word);
  1658.         memcpy(refs + i, &sref, sizeof(struct swapped_dylib_reference));
  1659.         }
  1660.     }
  1661.  
  1662. }
  1663.  
  1664. void
  1665. swap_dylib_module(
  1666. struct dylib_module *mods,
  1667. unsigned long nmods,
  1668. enum byte_sex target_byte_sex)
  1669. {
  1670.     unsigned long i;
  1671.  
  1672.     for(i = 0; i < nmods; i++){
  1673.         mods[i].module_name = SWAP_LONG(mods[i].module_name);
  1674.         mods[i].iextdefsym  = SWAP_LONG(mods[i].iextdefsym);
  1675.         mods[i].nextdefsym  = SWAP_LONG(mods[i].nextdefsym);
  1676.         mods[i].irefsym     = SWAP_LONG(mods[i].irefsym);
  1677.         mods[i].nrefsym     = SWAP_LONG(mods[i].nrefsym);
  1678.         mods[i].ilocalsym   = SWAP_LONG(mods[i].ilocalsym);
  1679.         mods[i].nlocalsym   = SWAP_LONG(mods[i].nlocalsym);
  1680.         mods[i].iextrel     = SWAP_LONG(mods[i].iextrel);
  1681.         mods[i].nextrel     = SWAP_LONG(mods[i].nextrel);
  1682.         mods[i].iinit       = SWAP_LONG(mods[i].iinit);
  1683.         mods[i].ninit       = SWAP_LONG(mods[i].ninit);
  1684.     }
  1685. }
  1686.  
  1687. void
  1688. swap_dylib_table_of_contents(
  1689. struct dylib_table_of_contents *tocs,
  1690. unsigned long ntocs,
  1691. enum byte_sex target_byte_sex)
  1692. {
  1693.     unsigned long i;
  1694.  
  1695.     for(i = 0; i < ntocs; i++){
  1696.         tocs[i].symbol_index = SWAP_LONG(tocs[i].symbol_index);
  1697.         tocs[i].module_index = SWAP_LONG(tocs[i].module_index);
  1698.     }
  1699. }
  1700.